package mediator

import "utilx/logx"

// Train 火车统一行为
type Train interface {
	arrive()
	leave()
	permitArrival()
}

// PassengerTrain 客运 -->中介者接口
type PassengerTrain struct {
	mediator Mediator
}

func (g *PassengerTrain) arrive() {
	if !g.mediator.CanArrive(g) {
		logx.Debug("PassengerTrain: Arrival blocked, waiting")
		return
	}
	logx.Debug("PassengerTrain: Arrived")
}

func (g *PassengerTrain) leave() {
	logx.Debug("PassengerTrain: Leaving")
	g.mediator.NotifyAboutDeparture()
}

func (g *PassengerTrain) permitArrival() {
	logx.Debug("PassengerTrain: Arrival permitted, arriving")
	g.arrive()
}

type FreightTrain struct {
	mediator Mediator
}

func (g *FreightTrain) arrive() {
	if !g.mediator.CanArrive(g) {
		logx.Warn("FreightTrain: Arrival blocked, waiting")
		return
	}
	logx.Warn("FreightTrain: Arrived")
}

func (g *FreightTrain) leave() {
	logx.Warn("FreightTrain: Leaving")
	g.mediator.NotifyAboutDeparture()
}

func (g *FreightTrain) permitArrival() {
	logx.Warn("FreightTrain: Arrival permitted")
	g.arrive()
}
